package org.xshaokuang.loginandemail;

import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.Config;
import org.xshaokuang.loginandemail.email.EmailDataManager;
import org.xshaokuang.loginandemail.email.EmailOpenCommand;
import org.xshaokuang.loginandemail.email.EmailSendCommand;
import org.xshaokuang.loginandemail.login.LoginDataManager;

public class MainPlugin extends PluginBase {

    public Config config;

    public Config emailData;

    public Config loginData;

    private static MainPlugin instance;

    private EmailDataManager emailDataManager;

    private LoginDataManager loginDataManager;

    public MainPlugin() {
    }

    public void onLoad() {
        instance = this;
        this.loadConfig();
        this.emailDataManager = new EmailDataManager();
        this.loginDataManager = new LoginDataManager(loginData);
    }

    public void onEnable() {
        this.getServer().getPluginManager().registerEvents(new MyListener(), this);
        this.registerCommands();
        this.getLogger().info("已启用");
    }

    public void onDisable() {
        this.saveConfig();
        this.getLogger().info("已关闭。");
        this.emailDataManager.saveAllToConfig();
        this.loginDataManager.saveAllToConfig();
    }

    public static MainPlugin getInstance(){
        return instance;
    }

    public EmailDataManager getEmailDataManager(){ return emailDataManager; }

    public LoginDataManager getLoginDataManager(){ return loginDataManager; }

    private void registerCommands() {
        this.getServer().getCommandMap().register("Xsk-Email", new EmailSendCommand(emailDataManager, loginData));
        this.getServer().getCommandMap().register("Xsk-Email", new EmailOpenCommand(emailDataManager));
    }

    private void loadConfig() {
        this.saveDefaultConfig();
        this.saveResource("config.yml");
        this.saveResource("email.yml");
        this.saveResource("account.yml");
        this.config = this.getConfig();
        this.emailData = new Config(this.getDataFolder() + "/email.yml");
        this.loginData = new Config(this.getDataFolder() + "/account.yml");
    }

    public void saveConfig() {
        this.config.save();
    }
}